package org.beian.fmmall.service.Impl;

import org.beian.fmmall.VO.ResStatus;
import org.beian.fmmall.VO.ResultVO;
import org.beian.fmmall.beans.ShoppingCart;
import org.beian.fmmall.beans.ShoppingCartVO;
import org.beian.fmmall.dao.ShoppingCartMapper;
import org.beian.fmmall.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Override
    public ResultVO addShoppingCart(ShoppingCart cart) {
        Example example = new Example(ShoppingCart.class);
        int i = shoppingCartMapper.insert(cart);
        if(i>0){
            return new ResultVO(ResStatus.OK,"success",null);
        }else{
            return new ResultVO(ResStatus.NO,"fail",null);
        }
    }

    @Override
    public ResultVO listShoppingCartByUserId(int userId) {
        List<ShoppingCartVO> shoppingCartVOS = shoppingCartMapper.selectShopCartByUserId(userId);
        return new ResultVO(ResStatus.OK,"success",shoppingCartVOS);
    }

    @Override
    public ResultVO updataCartNum(int cartId, int cartNum) {
        int i = shoppingCartMapper.updataCartNumByCartId(cartId, cartNum);
        if(i>0){
            return new ResultVO(ResStatus.OK,"success",null);
        }else{
            return new ResultVO(ResStatus.NO,"fail",null);
        }
    }

    @Override
    public ResultVO deleteCart(int cartId) {
        Example example = new Example(ShoppingCart.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cartId",cartId);
        int i = shoppingCartMapper.deleteByExample(example);
        if(i>0){
            return new ResultVO(ResStatus.OK,"success",null);
        }else{
            return new ResultVO(ResStatus.NO,"fail",null);
        }
    }

    @Override
    public ResultVO listShoppingCartByCids(String cids) {
        String[] arr=cids.split(",");
        List<Integer> cartIds=new ArrayList<>();
        for(int i=0;i<arr.length;i++){
            cartIds.add(Integer.parseInt(arr[i]));
        }
        List<ShoppingCartVO> shoppingCartVOS = shoppingCartMapper.selectShopCartByCids(cartIds);
        return new ResultVO(ResStatus.OK,"success",shoppingCartVOS);
    }
}
